//
//  DCFInputPhoneViewController.m
//  DCFactory
//
//  Created by 吴文拼 on 2018/3/31.
//  Copyright © 2018年 吴文拼. All rights reserved.
//

#import "DCFInputPhoneViewController.h"
#import <IQKeyboardManager.h>
#import "NSString+CommonUseTool.h"
#import "DCFAlertView.h"

@interface DCFInputPhoneViewController ()

@property (nonatomic , strong)DCFTextField *textField;

@end

@implementation DCFInputPhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    DCFNormalImageView *imV = [[DCFNormalImageView alloc] initWithImage:[UIImage imageNamed:@"top_img"]];
    [self.view addSubview:imV];
    [imV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.size.equalTo([NSValue valueWithCGSize:CGSizeMake(SCREEN_WIDTH, SCREEN_WIDTH/375.0*361.0)]);
    }];
    DCFNormalView *inputView = [[DCFNormalView alloc] init];
    [inputView set_dcf_layerCornerRadius:22.5f];
    inputView.layer.borderColor = kMainColor.CGColor;
    inputView.layer.borderWidth = 1.0f;
    [self.view addSubview:inputView];
    [inputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(imV.mas_bottom).offset(24);
        make.size.equalTo([NSValue valueWithCGSize:CGSizeMake(310, 45)]);
    }];
    UITextField *textfield = [[UITextField alloc] init];
    textfield.textColor = kMainColor;
    textfield.textAlignment = NSTextAlignmentLeft;
    textfield.font = GetFontWithSize(15);
    textfield.keyboardType = UIKeyboardTypeNumberPad;
    textfield.placeholder = @"请输入您的手机号码";
    [inputView addSubview:textfield];
    [textfield mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(inputView).offset(-10);
        make.left.equalTo(inputView).offset(10);
        make.top.bottom.equalTo(inputView);
    }];
    _textField = textfield;
    DCFButton *bottomBtn = [DCFButton buttonWithType:UIButtonTypeCustom];
    [bottomBtn set_dcf_Image:[UIImage imageNamed:@"bottom_btn"] forState:UIControlStateNormal];
    [bottomBtn addTarget:self action:@selector(bottomBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:bottomBtn];
    [bottomBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(inputView.mas_bottom).offset(24);
        make.size.equalTo([NSValue valueWithCGSize:CGSizeMake(300, 56)]);
    }];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    IQKeyboardManager *keyboardManager = [IQKeyboardManager sharedManager];
    keyboardManager.shouldResignOnTouchOutside = YES;
    keyboardManager.enable = YES;
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    IQKeyboardManager *keyboardManager = [IQKeyboardManager sharedManager];
    keyboardManager.shouldResignOnTouchOutside = NO;
    keyboardManager.enable = NO;
}

-(void)bottomBtnClick{
    NSString *phoneNum = _textField.text;
    if ([NSString isPhoneNumber:phoneNum]) {
        [DCFUserDefaults setObject:phoneNum forKey:@"FirstInputPhoneNumber"];
        [DCFUserDefaults synchronize];
        if (_completeBlock) {
            _completeBlock();
        }
    }else{
        DCFAlertView *alertView = [[DCFAlertView alloc] initWithDelegate:self withTitleText:@"提示" andDetailText:@"请输入正确的手机号码" andBottomBtnTitleArr:@[@"我知道了"]];
        [alertView showInView:self.view];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
